<!--
 * @Author: 秦福凯
 * @LastEditTime: 2025-11-21 16:19:21
 * @Description: 登录界面
-->
<template>
  <div class="u-login">
    <div class="login-box">
      <el-switch class="dark" v-model="globalStore.isDark" inline-prompt :active-icon="Sunny" :inactive-icon="Moon" @change="switchDark" />
      <div class="login-left">
        <img class="login-left-img" src="@/assets/img/login/login_left.png" alt="login" />
      </div>
      <div class="login-form">
        <div class="login-logo">
          <h2 class="logo-text">快投自媒体推广</h2>
        </div>
        <u-form @keyupEnter="login"   ref="refform" :form="form" :element="formList" @getUuid="getCaptcha"></u-form>
        <div class="login-btn">
          <el-button :icon="UserFilled" round size="large" type="primary" :loading="loading" @click="login">
            登录
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { useGlobalStore } from "@/store/global"
	import {useTheme} from '@/hooks/useTheme.js'
  import { Sunny, Moon,UserFilled } from '@element-plus/icons-vue'
  import { useTabsStore } from "@/store/tabs"
  import { useRouter } from "vue-router"
  import SystenApi from '@/api/system/index'
  import { reactive } from "vue"
  import { useUserStore } from "@/store/busi/user"
	const userStore = useUserStore()

  const useThemeMethod = useTheme();
  const globalStore = useGlobalStore();
  const tabsStore = useTabsStore();
  const router = useRouter()

  let loading = ref(false)
  let formList = reactive([
    {label:'账号:',type:'input',sp:24,lw:'70px',re:true,key:'account'},
    {label:'密码:',type:'password',sp:24,lw:'70px',re:true,key:'password'},
    // {label:'验证码:',type:'yzm',key:'code',lw:'70px',sp:24,re:true,src:''},
  ])
  let form = reactive({
    // account:'admin',
    // password:'admin123456',
    account:'',
    password:'',
    uuid:'',
    code:'',
  })
  // 切换模式
  function switchDark(){
    useThemeMethod.switchDark()
  }
  function login(){
    tabsStore.clearTabs();
    // router.push('/home');
      refform.value.soform.validate((vali) =>{
    if(!vali){
    }else{
      let parmas = {
        param:{
          ...JSON.parse(JSON.stringify(form)),
        }
      }
      loading.value = true

      // 租户 tenantLogin  管理员 adminLogin
      SystenApi.tenantLogin(parmas).then(res =>{
      loading.value = false
        if(res.data.code == 200){
          ElMessage.success('登录成功')
          localStorage.setItem('token',res.data.data.token)
          localStorage.setItem('nickName',res.data.data.nickName)
          userStore.userInfo.name = res.data.data.nickName
          router.push('/home');
          tabsStore.clearTabs();
        }else{
          ElMessage.warning(res.data.msg)
          getCode()
        }
    }).catch(err =>{
      loading.value = false
      getCaptcha()
    })
    }
  })
  }
  
  const refform = ref(null)
  let segments = ref([])
  let result = ref([])

   // 生成随机大写字母
   const generateRandomLetter = () => {
    const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    return letters.charAt(Math.floor(Math.random() * letters.length));
  };

  // 生成一段4个字母
  const generateSegment = () => {
    let segment = '';
    for (let i = 0; i < 4; i++) {
      segment += generateRandomLetter();
    }
    return segment;
  };

    // 生成完整字符串
    const generateString = () => {
      segments.value = [];
      for (let i = 0; i < 6; i++) {
        segments.value.push(generateSegment());
      }
      result.value = segments.value.join('-');
      form.uuid = result.value 
    };

  

    // 获取验证码
  function getCaptcha(){
    return
    generateString()
    // let uuid = Array.from({ length: 6 }, () => Math.random().toString(36).substr(2, 4).toUpperCase()).join('-');
    SystenApi.captcha(result.value).then(res =>{
      if( res && res.data.code == 200){
        formList[formList.findIndex(i => i.key == 'code')].src  = "data:image/gif;base64," + res.data.data;
      }else{
        ElMessage.warning(res.data.msg)
      }
    })
  }
  getCaptcha()
</script>

<style lang="scss">
  .u-login{
    height: 100vh;
    min-height: 550px;
    background-color: #eeeeee;
    background-image: url("@/assets/img/login/login_bg.svg");
    background-size: 100% 100%;
    background-size: cover;
    display: flex;
    align-items: center;
    justify-content: center;
    .login-box{
      position: relative;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: space-around;
      width: 96.5%;
      height: 94%;
      padding: 0 50px;
      background-color: rgb(255 255 255 / 80%);
      border-radius: 10px;
      .dark {
      position: absolute;
      top: 13px;
      right: 18px;
      }
      .login-left {
        width: 800px;
        margin-right: 10px;
        .login-left-img {
          width: 100%;
          height: 100%;
        }
     }
     .login-form {
      width: 420px;
      padding: 40px 40px 45px;
      background-color: var(--el-bg-color);
      border-radius: 10px;
      box-shadow: rgb(0 0 0 / 10%) 0 2px 10px 2px;
      .login-logo {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 45px;
        .login-icon {
          width: 60px;
          height: 52px;
        }
        .logo-text {
          padding: 0 0 0 25px;
          margin: 0;
          font-size: 24px;
          font-weight: bold;
          color: #34495e;
          white-space: nowrap;
        }
      }
      .el-form-item {
        margin-bottom: 40px;
      }
      .login-btn {
        display: flex;
        width: 100%;
        justify-content: center;
        .el-button {
          width: 90%
        }
      }
    }
    }
  }
</style>